//
//  ByRecommendListVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/22.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByRecommendListVC.h"
#import "ByCoreViewSegmentControl.h"
#import "ByBetRecordCell.h"
#import "ByBetRecordDetailVC.h"
#import "ByBallBetDetailVC.h"
#import "BySuperLottoVC.h"
#import "ByDoubleBallVC.h"
#import "ByFootBallBetVC.h"
#import "ByMyDocumentaryListVC.h"
#import "ByMyDocumentaryListCell.h"
#import "ByMyDocumentaryDetailVC.h"

@interface ByRecommendListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) ByCoreViewSegmentControl *segTitle;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger type;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) ByPayBombView *payBombView;
@property (nonatomic, strong) ByNetModel *orderModel;

@end

@implementation ByRecommendListVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}


- (ByPayBombView *)payBombView{
    if (_payBombView == nil) {
        _payBombView = [[ByPayBombView alloc] init];
        
        __weak typeof(self) weakSelf = self;
        _payBombView.sureBlock = ^(){
           
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            [weakSelf loadData];
            
        };
        
        
        _payBombView.otherSureBlock = ^(NSString *str,NSInteger type){
            
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            
            if (type == 1) {
                ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:str];
                vc.titleString = @"支付宝支付";
                [weakSelf by_normalPush:vc hasNavi:YES];
            }else{
                ByWechatPayQrView *vc = [[ByWechatPayQrView alloc] init];
                vc.title = @"微信支付";
                vc.str = str;
                [weakSelf by_normalPush:vc hasNavi:YES];
            }
            
        };
        
        _payBombView.selectBlock = ^(NSInteger index){
           
        };
    }
    return _payBombView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"我的推荐";
    [self initViews];
   
    
    self.type = 1;
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    
    
    [self addBYItemviews:@[@"我的跟单"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
     [self loadData];
}

- (void)rightItemsAction:(NSInteger)rightIndex{
    
    ByMyDocumentaryListVC *vc = [[ByMyDocumentaryListVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
    
}

- (void)loadData{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.type = self.type;
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    
    [ByDocumentaryApi followOrderMyPlanList:netModel success:^(ByNetBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert hideHud];
        SLog(@"跟单记录 = %@", result.data);
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in result.data[@"data"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            [self.dataArray addObject:netModel];
        }
        [self.tableView reloadData];
    } failure:^(ByNetBaseResult *result) {
        [self.dataArray removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert hideHud];
    }];
  
}


-(void)initViews{
    self.segTitle = [[ByCoreViewSegmentControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 39)];
    [self.view addSubview:self.segTitle];
    [self _initData];
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(39);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(39);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark -- 初始化数据 --
- (void)_initData{
    self.segTitle.sectionTitles = @[@"全部",@"待开奖",@"已中奖",@"未中奖"];
    self.segTitle.backgroundColor = [UIColor whiteColor];
    self.segTitle.font = FONT(16);
    self.segTitle.scrollEnabled = YES;
    self.segTitle.textColor = [UIColor blackColor];
    self.segTitle.selectedTextColor = BY_THEME_COLOR;
    self.segTitle.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segTitle.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segTitle.selectionIndicatorHeight = 3;
    self.segTitle.segmentEdgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
    __weak typeof(self) weakSelf = self;
    [self.segTitle setIndexChangeBlock:^(NSInteger index) {
        weakSelf.type = index + 1;
        [weakSelf loadData];
    }];
    [self.view addSubview:self.segTitle];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByMyDocumentaryListCell *cell = [ByMyDocumentaryListCell cellWithTableView:tableView];
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.isMyDocumentary = YES;
    cell.netModel = netModel;
    cell.rightBlock = ^(){
        //去支付
        if (netModel.pay_status.integerValue == 0 && netModel.sn) {
            self.orderModel = [netModel copy];
            self.payBombView.moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.total_amount];
            self.orderModel.amount = netModel.total_amount;
            self.payBombView.orderModel = self.orderModel;
            [self.payBombView show];
        }else{
            ByMyDocumentaryDetailVC *vc = [[ByMyDocumentaryDetailVC alloc] init];
            vc.netModel = netModel;
            vc.isMyDocumentary = YES;
            [self by_normalPush:vc hasNavi:YES];
        }
        
        

    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75 + 43 + 10;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    
    ByMyDocumentaryDetailVC *vc = [[ByMyDocumentaryDetailVC alloc] init];
    vc.netModel = netModel;
    vc.isMyDocumentary = YES;
    [self by_normalPush:vc hasNavi:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
